package Interfaces;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JTextArea;

public class AreaTesto
{
	JTextArea Campo; 
	String testo;
	
	public JTextArea crea(String t)
	{
		Campo = new JTextArea();
		Campo.setLineWrap(true);
		Campo.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		return crea_aux(t);
	}
	
	public JTextArea crea(String t, int n)
	{
		Campo = new JTextArea();
		Campo.setLineWrap(true);
		Campo.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		return crea_aux(t);
	}
	
	public JTextArea crea_aux(String t)
	{
		testo=t;
		Campo.setText(testo);
		Campo.addMouseListener(new MouseListener(){
			@Override public void mouseClicked(MouseEvent e) {
				if(Campo.getText().equals(testo))
					Campo.setText("");
			}
			@Override public void mouseEntered(MouseEvent e) {}
			@Override public void mouseExited(MouseEvent e) {}
			@Override public void mousePressed(MouseEvent e) {}
			@Override public void mouseReleased(MouseEvent e) {}
			});
		
		Campo.addKeyListener(new KeyListener(){
			@Override public void keyPressed(KeyEvent e) {}
			@Override public void keyReleased(KeyEvent e) {
			if(Campo.getText().equals(""))
				{
				Campo.setText(testo);
				Campo.selectAll();
				}				
			}
			@Override public void keyTyped(KeyEvent e) {}
			});
		return Campo;
	}
	public JTextArea get()
	{
		return Campo;
	}
	public void setText(String string)
	{
		Campo.setText(string);
	}
	public String getText()
	{
		return Campo.getText();
	}
	public void clear() 
	{
		Campo.setText(testo);
		
	}
}

